<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php


abstract class MApplicationComponent 
    extends MComponent 
    implements MIApplicationComponent
{
    private $_initialized = false;
    
    public function  __construct()
    {
        set_exception_handler(array($this, 'handleException'));
    }
    
    
    
    public function init()
    {
                                set_exception_handler(array($this, 'handleException'));
        set_error_handler(array($this, 'handleError'));
        
        $this->_initialized = true;
    }

    
    public function isInitialized()
    {
        return $this->_initialized;
    }
    
    
    public function handleError($code, $message, $file, $line)
    {
        return $this->displayError($code, $message, $file, $line);
    }
    
    
    public function handleException($exception)
    {
        $code = $exception->getCode();
        if (empty($code)){
            $code = 403;
        }
        header("HTTP/1.1 {$code} ". $exception->getMessage());
        return $this->displayException($exception);
    }
    
    
    public function displayException($exception)
    {
        $content = "";
        if (FXP_DEBUG)
        {
            $content = '<h1>'.get_class($exception)."</h1>\n";
            $content .= '<p>'.$exception->getMessage();
            $content .= ' ('.$exception->getFile().':'.$exception->getLine().')</p>';
            $content .= '<pre>'.$exception->getTraceAsString().'</pre>';
        }
        else
        {
            $content = '<h1>'.get_class($exception)."</h1>\n";
            $content .= '<p>'.$exception->getMessage().'</p>';
        }
        FXP::log($content, MLogger::LEVEL_ERROR);
        
                                $namespace = MAPIController::$namespace;
       
        $memo = $this->assembleMemo();
        $memo["line"] = $exception->getLine();
        $memo["file"]  = $exception->getFile();
        $memo["msg"]  = $exception->getMessage();
        $memo["trace"]  = $exception->getTraceAsString();
        MErrorLogs::createErrorLogs($namespace, json_encode($memo));
    }
    
    
    private function assembleMemo()
    {
        $memo = array();
        foreach ($_REQUEST as $key=>$value)
        {
            $memo[$key] = $value;
        }
        return $memo;
    }
    
    
    public function displayError($code, $message, $file, $line)
    {
        $content = "";
        if (FXP_DEBUG)
        {
            $content = "<h1>PHP Error [$code]</h1>\n";
            $content .= "<p>$message ($file:$line)</p>\n";
            $content .= '<pre>';

                                                $trace = debug_backtrace();
            if (count($trace) > 3)
                $trace = array_slice($trace,3);

            foreach ($trace as $i=>$t)
            {
                if (!isset($t['file']))
                    $t['file']='unknown';
                if (!isset($t['line']))
                    $t['line']=0;
                if (!isset($t['function']))
                    $t['function']='unknown';
                $content .= "#$i {$t['file']}({$t['line']}): ";

                if (isset($t['object']) && is_object($t['object']))
                    $content .= get_class($t['object']).'->';
                $content .= "{$t['function']}()\n";
            }

            $content .= '</pre>';
        }
        else
        {
            $content = "<h1>PHP Error [$code]</h1>\n";
            $content .= "<p>$message</p>\n";
        }
        FXP::log($content, MLogger::LEVEL_ERROR);
    }
}
?>